layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;

	var table_filter = 'ghghs_list',
		where_ghs_json = {
			_o: 'order_id',
			_b: 'desc',
			is_reset:'yes'
		},
		form_filter = 'ghghs_search_form';

	// 渲染表格
	layer.load();
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: order_list_url,
		method: 'post',
		height: 'full-200',
		where: {
			_o: 'order_id',
			_b: 'desc',
			is_reset:'yes'
		},
		defaultToolbar: ['filter'],
		totalRow: false,
		cols: [ghghs_cols],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 10,
			theme: '#393d49',
			groups: 8
		},
		autoSort: false,
		toolbar: '#orderGhsToolbar',
		initSort: {
			field: 'order_id',
			type: 'desc'
		},
		done: function() {
			layer.closeAll();
		}
	});

	form.on('checkbox(LAY_TABLE_TOOL_COLS)', function(data) {
		console.log(data.elem); //得到checkbox原始DOM对象
		console.log(data.elem.checked); //开关是否开启，true或者false
		console.log(data.value); //开关value值，也可以通过data.elem.value得到
		console.log(data.othis); //得到美化后的DOM对象
	})
	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		var add_url = $(this).attr('data-url');
		switch (obj.event) {
			
			case 'exportAllOrderGhs':
				layer.confirm('确定导出本次查询的订单吗？', {
					yes: function() {
						layer.load(2);
						send_info(order_export_url, where_ghs_json, function(data) {
							layer.closeAll();
							layer.open({
								type: 2,
								title: '下载excel',
								content: data.data
							});
						})
					}
				});
				break;
			default:
				break;
		}
		return false;
	})

	// 点击查看详情
	table.on('row(' + table_filter + ')', function(obj) {
		var get_site_id = $('form[lay-filter="order_search_form"]').find('select[name="site_id"]')
			.val(),
			content_url = order_detail_url + '?order_no=' + obj.data.order_no;
		if (get_site_id != undefined && get_site_id != '') {
			content_url = content_url + '&site_id=' + get_site_id;
		}
		var node_obj = document.elementFromPoint(event.clientX, event.clientY);
		if ($(node_obj).hasClass('layui-icon-ok') == false) {
			layer.open({
				type: 2,
				anim: -1,
				title: '订单详情',
				closeBtn: true,
				offset: 'r',
				shade: 0.3,
				move: false,
				shadeClose: false,
				area: '90%',
				skin: "layui-anim layui-anim-rl layui-layer-adminRight",
				content: content_url
			})
		}
		return false;
	})




	// 排序
	table.on('sort(' + table_filter + ')', function(data) {
		var _o = data.field;
		var _b = data.type;
		table.reload(table_filter, {
			initSort: data,
			where: {
				_o: _o,
				_b: _b
			}
		})
	})
	form.render();
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		where_ghs_json[name] = value;
		/*
		layer.load();
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			},
			done: function() {
				layer.closeAll();
			}
		});
		*/
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	});
	$('#order_ghghs_search_btn').on('click',function(){
		layer.load();
		table.reload(table_filter, {
			where: where_ghs_json,
			page: {
				curr: 1
			},
			done: function() {
				layer.closeAll();
			}
		});
	})
	var select_search = function(field, value) {
		var where = {};
		where[field] = value;
		where_ghs_json[field] = value;
		/*
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
		*/
	};
	// 交货时间
	var delivery_date = laydate.render({
		elem: 'input[name="order_form_delivery_date"]',
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search('delivery_date', value);
		}
	});
	// 指派时间
	var push_supplier_time_start = laydate.render({
		elem: 'input[name="push_supplier_time_start"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				push_supplier_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('push_supplier_time_start', value);
		}
	});
	var push_supplier_time_end = laydate.render({
		elem: 'input[name="push_supplier_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				push_supplier_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('push_supplier_time_end', value);
		}
	});
	// （供）确认时间
	var supplier_confirm_time_start = laydate.render({
		elem: 'input[name="supplier_confirm_time_start"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				supplier_confirm_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('supplier_confirm_time_start', value);
		}
	});
	var supplier_confirm_time_end = laydate.render({
		elem: 'input[name="supplier_confirm_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				supplier_confirm_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('supplier_confirm_time_end', value);
		}
	});
	// 下单时间
	var create_time_start = laydate.render({
		elem: 'input[name="order_form_create_time_start"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				create_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('create_time_start', value);
		}
	});
	var create_time_end = laydate.render({
		elem: 'input[name="order_form_create_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				create_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('create_time_end', value);
		}
	});
	// 确认时间
	var confirm_time_start = laydate.render({
		elem: 'input[name="order_form_confirm_time_start"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				confirm_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('confirm_time_start', value);
		}
	});
	var confirm_time_end = laydate.render({
		elem: 'input[name="order_form_confirm_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				confirm_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('confirm_time_end', value);
		}
	});
	// 支付时间
	var pay_time_start = laydate.render({
		elem: 'input[name="order_form_pay_time_start"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				pay_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('pay_time_start', value);
		}
	});
	var pay_time_end = laydate.render({
		elem: 'input[name="order_form_pay_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				pay_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('pay_time_end', value);
		}
	});
	// 发货时间
	var shopping_time_start = laydate.render({
		elem: 'input[name="order_form_shopping_time_start"]',
		max: 0,
		theme: '#393D49',

		done: function(value, date, endDate) {
			if (value) {
				shopping_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('shopping_time_start', value);
		}
	});
	var shopping_time_end = laydate.render({
		elem: 'input[name="order_form_shopping_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				shopping_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('shopping_time_end', value);
		}
	});
	// 收货时间
	var receipt_time_start = laydate.render({
		elem: 'input[name="order_form_receipt_time_start"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				receipt_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('receipt_time_start', value);
		}
	});
	var receipt_time_end = laydate.render({
		elem: 'input[name="order_form_receipt_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				receipt_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('receipt_time_end', value);
		}
	});
	// 供发货时间
	var gys_fh_time_start = laydate.render({
		elem: 'input[name="gys_fh_time_start"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				gys_fh_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('gys_fh_time_start', value);
		}
	});
	var gys_fh_time_end = laydate.render({
		elem: 'input[name="gys_fh_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				gys_fh_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('gys_fh_time_end', value);
		}
	});
	// 军发货时间
	var jc_fh_time_start = laydate.render({
		elem: 'input[name="jc_fh_time_start"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				jc_fh_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('jc_fh_time_start', value);
		}
	});
	var jc_fh_time_end = laydate.render({
		elem: 'input[name="jc_fh_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				jc_fh_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('jc_fh_time_end', value);
		}
	});
	// 供收货时间
	var gys_sh_time_start = laydate.render({
		elem: 'input[name="gys_sh_time_start"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				gys_sh_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('gys_sh_time_start', value);
		}
	});
	var gys_sh_time_end = laydate.render({
		elem: 'input[name="gys_sh_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				gys_sh_time_end.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('gys_sh_time_end', value);
		}
	});
	// 军收货时间
	var jc_sh_time_start = laydate.render({
		elem: 'input[name="jc_sh_time_start"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				jc_sh_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('jc_sh_time_start', value);
		}
	});
	var jc_sh_time_end = laydate.render({
		elem: 'input[name="jc_sh_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				jc_sh_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('jc_sh_time_end', value);
		}
	});

	form.on('select', function(data) {
		var value = data.value;
		var e = data.elem;
		var id = $(e).attr('id');
		switch (id) {
			case 'site_id':
				select_search('site_id', value);
				break;
			case 'order_type':
				select_search('order_type', value);
				break;
			default:
				break;
		}
	});
	form.on('radio', function(data) {
		var value = data.value;
		var e = data.elem;
		var name = $(e).attr('name');
		select_search(name, value);
	});
	var edit_order = function(field, value, order_id) {
		var post = '{"order_id":"' + order_id + '","' + field + '":"' + value + '"}';
		var data = JSON.parse(post);
		send_info(order_save_url, data, function(data) {
			if (data.code == -1) {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			} else {
				layer.msg('修改成功', {
					icon: 1
				});
			}
		})
	}
	//监听编辑
	table.on('edit(' + table_filter + ')', function(obj) {
		var field = obj.field,
			value = obj.value,
			order_id = obj.data.order_id;
		order_adsense(field, value, order_id);
	});
	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var order_id = data.order_id;
		switch (e) {
			case 'option_log':
				layer.open({
					type: 2,
					anim: -1,
					title: '操作日志',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: true,
					area: '30%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: option_log_url + '?order_no='+data.order_no
				})
				break;
			case 'del':
				// 删除
				layer.confirm('确定删除吗？', function(index) {

					send_info(order_delete_url, {
						order_id: order_id
					}, function(data) {
						if (data.code == 0) {
							obj.del();
							layer.msg('删除成功', {
								icon: 1
							});
							// 重载回收站
							if ($('table[lay-filter="recycle_order_list"]').length >
								0) {
								table.reload('recycle_order_list');
							}
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'edit':
				create_tab($(this));
				break;
			case 'update':
				send_info(order_one_url, {
					order_id: data.order_id
				}, function(data) {
					delete data.data.order_id;
					obj.update(data.data);
				})
				break;
			default:
				break;
		}
	})
});
